#-*- coding:utf-8 -*-
import os
import sys
import threading
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class DetectiveThread(QThread):

    def __init__(self,dir_path,project_name):
        super(DetectiveThread, self).__init__()
        self.dir_path = dir_path
        self.project_name = project_name
        self.size = 0

        self.project_sizes = []

    def run(self):
        self.size = self.getdirsize(self.dir_path)

        project_path = os.path.join(self.dir_path, self.project_name)
        if os.path.isdir(project_path):
            asset_shots = os.listdir(project_path)

            shot_info_list = []
            all_shot_size = 0
            for s in asset_shots:
                shot_path = os.path.join(project_path, s)
                if os.path.isdir(shot_path):
                    shot_size = self.getdirsize(shot_path)
                    shot_info = f'{s}   大小:{shot_size}M'
                    shot_info_list.append(shot_info)
                    all_shot_size += shot_size
            project_size = round(all_shot_size, 2)
            self.project_sizes[f'{self.project_name}   大小:{project_size}M'] = shot_info_list


    def getdirsize(self,dir_path):
        size = 0
        for root, dirs, files in os.walk(dir_path):
            size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
        last_size = round(size / 1024 / 1024,2)
        return last_size


class ProjectWindow(QWidget):

    def __init__(self):
        super(ProjectWindow, self).__init__()

        self.data = {}
        self.setWindowTitle('Size')
        self.main_layout = QHBoxLayout()
        self.project_layout = QVBoxLayout()
        self.project_label = QLabel('项目')
        self.project_list = QListWidget()
        self.project_list.clicked.connect(self._show_size)
        self.project_layout.addWidget(self.project_label)
        self.project_layout.addWidget(self.project_list)
        self.shot_layout = QVBoxLayout()
        self.shot_label = QLabel('资产/镜头')
        self.shot_list = QListWidget()
        self.shot_layout.addWidget(self.shot_label)
        self.shot_layout.addWidget(self.shot_list)
        self.main_layout.addLayout(self.project_layout)
        self.main_layout.addLayout(self.shot_layout)
        self.setLayout(self.main_layout)

    def set_info(self,info):
        self.data = info
        self.project_list.clear()
        self.shot_list.clear()
        if info:
            self.project_list.addItems(info.keys())


    def _show_size(self,index):
        self.shot_list.clear()
        item = self.project_list.itemFromIndex(index)
        project_key = item.text()
        shot_size_list = self.data[project_key]
        self.shot_list.addItems(shot_size_list)


class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.detect_thread_list = []
        self.setWindowTitle('项目大小检测')
        self.main_layout = QVBoxLayout()
        self.main_label = QLabel('选择路径')
        self.path_layout = QHBoxLayout()
        self.path_edit = QLineEdit()
        self.path_btn = QPushButton('path')
        self.path_btn.clicked.connect(self._choose_dir)
        self.OK_btn = QPushButton('OK')
        self.OK_btn.clicked.connect(self._choose)
        self.path_layout.addWidget(self.path_edit)
        self.path_layout.addWidget(self.path_btn)
        self.path_layout.addWidget(self.OK_btn)
        self.main_layout.addWidget(self.main_label)
        self.main_layout.addLayout(self.path_layout)
        self.setLayout(self.main_layout)

    def _choose_dir(self):
        c_dir=QFileDialog.getExistingDirectory(self,'选择目录')

        self.path_edit.setText(c_dir)

    def _choose(self):
        if self.thread_list:
            for t in self.thread_list:
                t.quit()
                t.wait()
        try:
            dir_path = self.path_edit.text()
            if dir_path:
                project_sizes = {}
                projects = os.listdir(dir_path)
                for p in projects:
                    self.det_thread = DetectiveThread(dir_path,p)
                    self.detect_thread_list.append(self.det_thread)
                    self.det_thread.start()
                    self.det_thread.


                self.p = ProjectWindow()
                self.p.set_info(project_sizes)
                self.p.show()
        except Exception as ERR:
            print(ERR)

    # def _choose(self):
    #     try:
    #         dir_path = self.path_edit.text()
    #         if dir_path:
    #             project_sizes = {}
    #             projects = os.listdir(dir_path)
    #             for p in projects:
    #                 project_path = os.path.join(dir_path,p)
    #                 if os.path.isdir(project_path):
    #                     asset_shots = os.listdir(project_path)
    #                     # shots = os.listdir(asset_shots)
    #                     shot_info_list = []
    #                     all_shot_size = 0
    #                     for s in asset_shots:
    #                         shot_path = os.path.join(project_path,s)
    #                         if os.path.isdir(shot_path):
    #                             shot_size = self.getdirsize(shot_path)
    #                             shot_info = f'{s}   大小:{shot_size}M'
    #                             shot_info_list.append(shot_info)
    #                             all_shot_size+=shot_size
    #                     project_size = round(all_shot_size,2)
    #                     project_sizes[f'{p}   大小:{project_size}M'] = shot_info_list
    #
    #             self.p = ProjectWindow()
    #             self.p.set_info(project_sizes)
    #             self.p.show()
    #     except Exception as ERR:
    #         print(ERR)
    #         # self.close()
    #




if __name__ == '__main__':
    app = QApplication(sys.argv)
    m = MainWindow()
    m.show()
    sys.exit(app.exec_())
